/*
 * Copyright (c) 2015 smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.dao.base;

import java.io.Serializable;
import java.util.List;

/**
 * dao实现类
 * @author KongXiangyu
 * @since 2015-08-05
 * @param <T> 实体
 * @param <KEY> 主键
 */
public abstract class BaseDaoImpl<T, KEY extends Serializable> extends MyBatisSupport implements BaseDao<T, KEY> {

    private static final String DEFAULT_INSERT_KEY = "insertEntry";
    private static final String DEFAULT_INSERT_LAST_SEQUENCE_KEY = "lastSequence";
    private static final String DEFAULT_DELETE_ARRAY_KEY = "deleteByArrayKey";
    private static final String DEFAULT_DELETE_CONDTION = "deleteByCondtion";
    private static final String DEFAULT_UPDATE_KEY = "updateByKey";
    private static final String DEFAULT_SELECT_ARRAY_KEY = "selectEntryArray";
    private static final String DEFAULT_SELECT_CONDTION = "selectEntryList";
    private static final String DEFAULT_SELECT_CONDTION_COUNT = "selectEntryListCount";

    /**
     * 获取命名空前前缀
     * @param statement
     * @return
     */
    public abstract String getNameSpace(String statement);

    @SuppressWarnings("unchecked")
    public int insertEntry(T... t) {
        int result = 0;
        int count = 0;
        Long lastId = 0L;
        if (t == null || t.length <= 0) {
            return result;
        }
        for (T o : t) {
            if (o != null) {
                count = this.insert(getNameSpace(DEFAULT_INSERT_KEY), o);
                if (count > 0) {
                    result += count;
                    lastId = this.lastSequence();
                    if (lastId != null && lastId > 0) {
                        try {
                            Class<?> clz = o.getClass();
                            clz.getMethod("setId", Long.class).invoke(o, lastId);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return result;
    }

    @Override
    public long lastSequence() {
        return select(getNameSpace(DEFAULT_INSERT_LAST_SEQUENCE_KEY), null);
    }

    @SuppressWarnings("unchecked")
    public int deleteByKey(KEY... id) {
        return this.delete(getNameSpace(DEFAULT_DELETE_ARRAY_KEY), id);
    }

    public int deleteByKey(T t) {
        return this.delete(getNameSpace(DEFAULT_DELETE_CONDTION), t);
    }

    public int updateByKey(T t) {
        return this.update(getNameSpace(DEFAULT_UPDATE_KEY), t);
    }

    public T selectEntry(KEY id) {
        List<T> list = this.selectEntryList(id);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public List<T> selectEntryList(KEY... ids) {
        if (ids == null || ids.length <= 0) {
            return null;
        }
        return this.selectList(getNameSpace(DEFAULT_SELECT_ARRAY_KEY), ids);
    }

    public List<T> selectEntryList(T t) {
        return this.selectList(getNameSpace(DEFAULT_SELECT_CONDTION), t);
    }

    public Integer selectEntryListCount(T t) {
        return this.select(getNameSpace(DEFAULT_SELECT_CONDTION_COUNT), t);
    }
}
